package com.kylin.module.community.orm;

import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.common.exception.BusinessException;
import com.kylin.module.user.orm.User;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.kylin.module.community.orm.Friendslist;
import org.hibernate.Query;
import java.util.Date;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/*
import org.hibernate.LockMode;
import org.hibernate.criterion.Example;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.exception.SystemException;
*/

/**
 * Data access object (DAO) for domain model class Friendslist.
 * @see 
 * @author mjj
 */
public class FriendslistDAO extends BaseHibernateDAO {

	
    private static final Log log = LogFactory.getLog(MemberDAO.class); 

    public void setTotalbyC(Long userid){
    	getSession().createCriteria(Friendslist.class)
    	.add(Restrictions.eq("active",new Integer("1")))
    	.add(Restrictions.eq("userid",userid))
    	.setProjection( Projections.rowCount() );
    }
    
    public Integer getTotal(User user){
    	String queryString = "select count(*) from Friendslist where   active=1 and user = ? ";
        Query query = getSession().createQuery(queryString);
        query.setEntity(0, user);  
        log.info("total: "+query.list().get(0));
        if (query.list() != null ){
        	return  new Integer ( query.list().get(0).toString());
        }
        else{
        	return null;        	
        }
    }
    
    public List find(User user) {
        log.info("finding friends");
        try {
           String queryString = "from Friendslist   where   active=1 and user = ? order by  createdon desc";        	   
           Query query = getSession().createQuery(queryString);
           query.setEntity(0, user);
           query.setFirstResult(0);
           query.setMaxResults(10);
  		 //query.setParameter(0, value);
           log.info("size: "+ query.list().size());
  		 return query.list();
        } catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
        }
  	}
    
    /*
     * 
     * 
     * 
     */
    
    public List getList(User user, Integer limitstart, Integer limit) {
        log.info("finding friends of " + user.getUsername() );
        try {
            String queryString = "from Friendslist as f where  f.active=1 and f.user = ? order by f.createdon desc";
            Query query = getSession().createQuery(queryString);
            query.setEntity(0, user);
            query.setFirstResult(limitstart.intValue());
            query.setMaxResults(limit.intValue());
   		 //query.setParameter(0, value);
            log.info("size: "+ query.list().size());
            log.info("sql: "+ query.getQueryString());
   		 return query.list();
        }
        catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
           
        }
  	}
    
    public List list(User user) {
        log.info("finding friends of " + user.getUsername() );
        try {
            String queryString = "from Friendslist as f where  f.active=1 and f.user = ? order by f.createdon desc";
            Query query = getSession().createQuery(queryString);
            query.setEntity(0, user);
            query.setFirstResult(0);
            query.setMaxResults(10);
   		 //query.setParameter(0, value);
            log.info("size: "+ query.list().size());
            log.info("sql: "+ query.getQueryString());
   		 return query.list();
        }
        catch (RuntimeException re) {
           log.error("find by property name failed", re);
           throw re;
           
        }
  	}
    
    /*
     * 
     * 
     * 
     */
   public void add(User user, User user2){
	   log.info("adding friend");
	   Friendslist flist = new Friendslist();
	   
	   flist.setUser(user);
	   flist.setUser2(user2);
	   
	   flist.setCreatedon(new Date());
	   flist.setActive(new Short("1"));
	   flist.setHits(new Integer(0));	   
	   
	   try{
		   super.save(flist);
	   }
	   catch(BusinessException be){
		   log.error("adding friend"+be.toString());   
	   }

   }
   
   /*
    * 
    * 
    * 
    */
   public void edit(Long id){
	   Friendslist flist = null;
	   try{
	    flist = (Friendslist)super.findById(Friendslist.class, id);
	   }
		catch(BusinessException be){
    		log.info("finding friends");
    	}
		if (flist != null){
			flist.setActive(new Short("0"));
			try{
			super.saveOrUpdate(flist);
			}
			catch(BusinessException be){
	    		log.info("update friend failed" + be.toString());
	    	}

		}
    	   
   }
	   
   /*
    * 
    * 
    */
	public void delete(User user, User user2){
		
        log.info("finding friends");
        List list = null;
      
           String queryString = "from Friendslist as f where  f.active=1 and f.user = ? and f.user2 = ?";
           Query query = getSession().createQuery(queryString);
           query.setEntity(0, user);
           query.setEntity(1, user2);
           log.info("size: "+ query.list().size());
  		   list =  query.list();

        Friendslist flist = null;
        for(int i=0; i<list.size();i++){
        	  flist = (Friendslist)list.get(i);
        	try{
        	super.delete(flist);
        	}
        	catch(BusinessException be){
        		log.info("finding friends");
        	}
        	
        }	   
   }
    
}
